﻿using EntityPet;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PET.Tools;

namespace PET.Controllers
{
    /// <summary>
    /// 外卖
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class FoodController : ControllerBase
    {
        private readonly PetDbContext db;

        public string? Dingdan { get; private set; }

        public FoodController(PetDbContext _context)
        {
            db = _context;

        }

        /// <summary>
        /// 查询所有订单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Result Get()
        {
            Result result = new Result();
            var resultData = db.Pet_TakeoutFood.ToList<Pet_TakeoutFood>();
            return result;
        }

        [HttpGet]
        public List<Pet_TakeoutFood> GetBySelect([FromHeader] string Model, [FromHeader] decimal Price, [FromHeader] string Color)
        {
            List<Pet_TakeoutFood> takeoutFood = new List<Pet_TakeoutFood>();

            if (string.IsNullOrEmpty(Dingdan))
            {
                takeoutFood = db.Pet_TakeoutFood.Where(s => s.Dingdan == Dingdan).OrderBy(s => s.Dingdan).ToList();
            }
            if (Price>0)
            {
                takeoutFood = db.Pet_TakeoutFood.Where(s => s.Price == Price).OrderBy(s => s.Price).ToList();
            }
            return takeoutFood;
        }

        /// <summary>
        /// 增加订单
        /// </summary>
        [HttpPost]
        public Result Add([FromBody] Pet_TakeoutFood food)
        {
            Result result = new Result();
            db.Pet_TakeoutFood.Add(food);
            result.Data = db.SaveChanges();
            return result;
        }

        /// <summary>
        /// 删除订单
        /// </summary>
        [HttpDelete]
        public Result Del(Guid id)
        {
            Result result = new Result();
            Pet_TakeoutFood? food = db.Pet_TakeoutFood.Where(w => w.ID == id).SingleOrDefault();
            if (food != null)
            db.Pet_TakeoutFood.Remove(food);
            result.Data = db.SaveChanges();
            return result;
        }

        /// <summary>
        /// 修改订单
        /// </summary>
        /// <param name="adopt"></param>
        /// <returns></returns>
        [HttpPut]
        public Result Up([FromBody] Pet_TakeoutFood food)
        {
            Result result = new Result();
            db.Pet_TakeoutFood.Update(food);
            result.Data = db.SaveChanges();
            return result;
        }
    }
}
